<?php


namespace app\api\model\page\user;


use app\api\model\dao\UserCurrencyExchange;
use app\library\Error;
use app\library\PageBase;

class GetUserCurrencyRecord extends PageBase
{
    private $objRecord;

    public function __construct()
    {
        parent::__construct();
        $this->objRecord = new UserCurrencyExchange();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $conds = [
            'user_id' => $this->user_id,
            'currency_type' => $param['type'],
        ];
        $fileds = ['amount', 'create_time', 'exchange_type'];
        $record = $this->objRecord->getExchangeRecord($conds, $fileds, 0, 1000, 'create_time');
        if (false === $record) {
            return $this->getRes(Error::ERR_SYS);
        }
        $this->data = $record;
        return $this->getRes();

    }

    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
            'type' => 'require|in:1,2',
        ];
        $msg = [
            'user_id.require' => '用户ID必填',
            'type.require' => '查询类型必填',
        ];

        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

}